package com.mall.biz.home;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.mall.MallGroupBuyBizImpl;
import com.mall.client.home.MallBannerService;
import com.mall.client.home.MallExtensionService;
import com.mall.client.mall.MallService;
import com.mall.client.mallCategory.MallCategoryService;
import com.mall.client.region.AreaService;
import com.mall.client.region.CityService;
import com.mall.client.user.UserExtendedService;
import com.mall.pojo.Area;
import com.mall.pojo.City;
import com.mall.pojo.Mall;
import com.mall.pojo.MallBanner;
import com.mall.pojo.MallCategory;
import com.mall.pojo.MallExtension;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.UserExtended;
import com.mall.query.MallBannerQuery;
import com.mall.query.MallExtensionQuery;
import com.mall.query.MallGroupBuyQuery;
import com.mall.query.Page;
import com.mall.vo.home.MallExtensionVO;
import com.mall.vo.home.MallGroupBuyVO;
import com.mall.vo.user.AliTonghua;

import net.sf.json.JSONObject;

@Service
public class HomeBizImpl {
	@Autowired
	private MallBannerService mallBannerService;
	
	@Autowired
	private MallExtensionService mallExtensionService;
	@Autowired
	private MallService mallService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private CityService cityService;
	@Autowired
	private MallCategoryService mallCategoryService;
	@Autowired
	private UserExtendedService userExtendedService;
	@Autowired
	private MallGroupBuyBizImpl mallGroupBuyBiz;

	public List<MallBanner> getMallBannerByOnlyKey(Integer onlyKey) {
		MallBannerQuery mallBannerQuery = new MallBannerQuery();
		Page bannerPage = Page.getInitPage(5);
		mallBannerQuery.setPage(bannerPage);
		mallBannerQuery.setOnlyKey(onlyKey);
		mallBannerQuery.setType(0);
		mallBannerQuery.setStatus(0);
		List<MallBanner> mallBannerList = this.mallBannerService.getMallBannerByMallQuery(mallBannerQuery);
		return mallBannerList;
	}

	public List<MallBanner> getMallBannerGGByOnlyKey(Integer onlyKey) {
		MallBannerQuery mallBannerQuery = new MallBannerQuery();
		Page bannerPage = Page.getInitPage(1);
		mallBannerQuery.setPage(bannerPage);
		mallBannerQuery.setOnlyKey(onlyKey);
		mallBannerQuery.setType(2);
		mallBannerQuery.setStatus(0);
		List<MallBanner> mallBannerList = this.mallBannerService.getMallBannerByMallQuery(mallBannerQuery);
		return mallBannerList;
	}


	public List<MallExtensionVO> getMallExtensionVOListByOnlyKeyAndType(Integer onlyKey, Integer i) {
		MallExtensionQuery mallExtensionQuery = new MallExtensionQuery();
		Page extensionPage = Page.getInitPage(3);
		mallExtensionQuery.setPage(extensionPage);
		mallExtensionQuery.setOnlyKey(onlyKey);
		mallExtensionQuery.setStatus(i);
		mallExtensionQuery.setType(0);
		List<MallExtension> mallExtensionList = this.mallExtensionService
				.getMallExtensionByMallExtensionQuery(mallExtensionQuery);
		List<MallExtensionVO> mallExtensionVOList = new ArrayList<MallExtensionVO>();
		MallExtensionVO mallExtensionVO = null;
		for (MallExtension mallExtension : mallExtensionList) {
			mallExtensionVO = new MallExtensionVO();
			Mall mall = this.mallService.getMallById(mallExtension.getMallId());
			City city = null;
			if(mallExtension.getCityId()>0 && mallExtension.getCityId() != null ){
				 city = this.cityService.getCityById(mallExtension.getCityId());
			}
			Area area = null;
			if( mallExtension.getAreaId() != null && mallExtension.getAreaId()>0){
				area = this.areaService.getAreaByAreaId(mallExtension.getAreaId());
			}
			mallExtensionVO.setMallExtension(mallExtension);
			mallExtensionVO.setMall(mall);
			mallExtensionVO.setCity(city);
			mallExtensionVO.setArea(area);
			mallExtensionVOList.add(mallExtensionVO);
		}
		return mallExtensionVOList;
	}
	
	
	public List<MallGroupBuyVO> getMallGroupBuyVOByQuery(MallGroupBuyQuery mallGroupBuyQuery) {
		List<MallGroupBuyVO> mallGroupBuyVOList = new ArrayList<>();
		List<MallGroupBuy> mallGroupBuyList = mallGroupBuyBiz.getMallGroupBuyListByQuery(mallGroupBuyQuery);
		if (mallGroupBuyList == null || mallGroupBuyList.size() <= 0) {
			return mallGroupBuyVOList;
		}
		MallGroupBuyVO mallGroupBuyVO = null;
		MallCategory secMallCategory = null;
		MallCategory parentMallCategory = null;
		Set<Long> mallIdSet = new HashSet<>();
		Mall mall = null;
		for (MallGroupBuy mallGroupBuy : mallGroupBuyList) {
			mallGroupBuyVO = new MallGroupBuyVO();
			mallGroupBuyVO.setMallGroupBuy(mallGroupBuy);
			mall = this.mallService.getMallById(mallGroupBuy.getMallId());
			if (mallGroupBuyQuery.getDoset()) {
				if (mallIdSet.contains(mall.getId())) {
					continue;
				} else {
					mallIdSet.add(mall.getId());
				}
			}
			mallGroupBuyVO.setMall(mall);
			secMallCategory = this.mallCategoryService.getMallCategoryById(mallGroupBuy.getSecCategoryId());
			parentMallCategory = this.mallCategoryService.getMallCategoryById(mallGroupBuy.getCategoryId());
			mallGroupBuyVO.setParentMallCategory(parentMallCategory);
			mallGroupBuyVO.setSecMallCategory(secMallCategory);
			if(mallGroupBuy.getCityId() != null && mallGroupBuy.getCityId()>0){
				mallGroupBuyVO.setCity(cityService.getCityById(mallGroupBuy.getCityId()));
			}
			if(mallGroupBuy.getAreaId()!= null && mallGroupBuy.getAreaId()> 0 ){
				mallGroupBuyVO.setArea(areaService.getAreaByAreaId(mallGroupBuy.getCityId()));
			}
			mallGroupBuyVOList.add(mallGroupBuyVO);
		}
		return mallGroupBuyVOList;
	}

	public AliTonghua getSellerALiUserInfo(Mall mall){
		AliTonghua aliUserInfo = null;
		Long sellerId = mall.getSellerId();//卖家id
		if(sellerId != null){
			UserExtended user = this.userExtendedService.getUserExtendedByUserId(sellerId);
			if(user != null){
				String sellerAliInfo = user.getAliTongxun();
				
				if (StringUtils.isNotBlank(sellerAliInfo)) {
					JSONObject aliuserInfJson = JSONObject.fromObject(sellerAliInfo);
					aliUserInfo = (AliTonghua) JSONObject.toBean(aliuserInfJson, AliTonghua.class);
				}
			}
		}
		return aliUserInfo;
	}
	
	//分页后得到的页数
	public Integer getPages(Integer length,Integer pageSize){
		Integer pages = 1;
		if(length != null && length > 0){
			pages = length%pageSize;
			if(pages > 0){
				pages = length/pageSize+1;
			}
		}
		return pages;
	}
	
	//获取店铺的类目对象
	public MallCategory getMallCategoryByCategoryId(Integer mallCategoryId){
		MallCategory mallCategory = null;
		if(mallCategoryId == null || mallCategoryId <= 0 ){
			return mallCategory;
		}
		mallCategory = this.mallCategoryService.getMallCategoryById(mallCategoryId);
		return mallCategory;
	}
}
